home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: very simple newbie question
- Date: 29 Feb 1996 20:30:30 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb29133030@qcd.lanl.gov>
- References: <robertk-2802960012350001@robertk.accessone.com>
- <TANMOY.96Feb28090444@qcd.lanl.gov> <3135B987.1DDE@ix.netcom.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Norman Bullen's message of Thu, 29 Feb 1996 06:34:47 -0800
-
- In article <3135B987.1DDE@ix.netcom.com>
- Norman Bullen <nbullen@ix.netcom.com> writes:
-
- <snip>
- NB: > r: scanf("%s/n",s);
- <snip>
- NB: > First, /n is not white space, it is the character '/' followed by the
- NB: > character 'n'. When /n appears as a part of the format string for
- NB: > scanf, it tells scanf to read and discard the characters / and n if
- NB: > they follow. As the end of line is not a /, the behaviour is not
- NB: > consistent with your code. Check your code before wasting people's
- NB: > time with it.
- NB: >
- NB: > Next, whitespace is not ignored in scanf. It means read and discard
- NB: > all following whitespace. So, scanf has to continue reading till it
- NB: > finds a non-whitespace character, which it leaves unread. This
- NB: > explains the behaviour that you see.
- NB: >
- NB: > Cheers
- NB: > Tanmoy
- NB: > --
- NB: What he's trying to say (but doesn't seem to express very well) is that
- NB: the representation of a "newline" character should have a backward slash
- NB: (the top end pointed left) instead of a forward slash.
-
- No, what I am trying to say is that /n is valid in scanf, and I
- explain what is done. I also pointed out that people should check
- their code before posting worldwide. I also explained what \n in its
- stead would have done.
-
- NB:
- NB: That's not the real problem, however. scanf is a really poorly conceived
- NB: function, IMHO. It rules are too complicated and it does not do what most
- NB: people want it to do. For your situation, as shown in the sample code
- NB: above, you want to use gets instead of scanf.
-
- If _you_ don't find something easy to understand, does not mean it is
- ill-conceived. If I don't understand, well it is :-). Joking aside,
- scanf is very powerful, and I often dispense off the phase of reading a
- line with fgets (if you are advising people to use gets, you haven't
- read the FAQ). I certainly prefer scanf("%*[^\n]"); scanf("%*c"); over
- the equivalent fgets.
-
- scanf is much maligned, but is easy to understand, compact, can be
- used safely and easy to read. Why should one advise against it?
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-